angular.module("application")
.controller('videoCtrl',['$scope', '$http', '$q', 'BASE_URL', 'renderHtml', 'catProcess',
	function($scope, $http, $q, BASE_URL, renderHtml, catProcess) 
{	
	// Use for simpleFilter
	$scope.filterType = "description";

	$scope.catFilter = false;
	$scope.region = 'tg';
	$scope.videoName = "";
	$scope.catId = "";
	$scope.videosList = [];

	$http.post(BASE_URL + 'index.php/application_api/multimedia/getvideos')
	.then(function(promise) {

		var checkAsync = [];
		angular.forEach(promise.data, function(video, i) {
			checkAsync.push(
				$http.get('http://gdata.youtube.com/feeds/api/videos/' + video._id)
				.then(function(prm) {
					$scope.videosList.push(video);
				}, function(err) {
					if(err.data != 'Private video') {
						$scope.videosList.push(video);
					}
				})
			);				
		})
		$q.all(checkAsync).then(function() {
			setTimeout(function() {
				$scope.$apply(function() {
					$scope.$parent.loadingView = false;
				})
			},700);
			setTimeout(function() {
				renderHtml.makePopup();
				renderHtml.addIcon();
			}, 800);
		})

		return catProcess.getCats();
	}).then(function(promise) {
		$scope.categories = renderHtml.renderSelectCat(promise);
	})

	$scope.$watch('catFilter', function(newVal) {
		if(!newVal) $scope.catId = "";
		else {
			$scope.catId = "tg";
		}
	})
	$scope.$watch('region', function(newVal) {
		if($scope.catFilter) $scope.catId = newVal;
	})

	$scope.$watch('videoName + catId', function() {
		setTimeout(function() {
			renderHtml.makePopup();
			renderHtml.addIcon();
		},200);
	})


	zarr = ['o','ô','v','c','o1','o2','oz','oz1'];
	console.log(zarr.sort());
	
}])

// ==============================================================================================================
// ==============================================================================================================




// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================


.factory('renderHtml', function($http) {
	return {
		makePopup: function() {
			$('.youtube-popup').magnificPopup({type: "iframe"});
		},
		addIcon: function() {
			$('.youtube-popup').append('<span class="v-icon glyphicon glyphicon-play"></span>');
		},
		renderSelectCat: function(dt) {
			array = [];
			parentArr = [];
			childArr = [];
			angular.forEach(dt, function(cat) {
				if(!cat.parent) parentArr.push(cat);
				else {
					cat.name = ' -- ' + cat.name;
					childArr.push(cat);
				}
			});		
			angular.forEach(parentArr, function(par) {
				array.push(par);
				angular.forEach(childArr, function(child) {
					if(child.parent == par._id) {
						array.push(child);
					}
				});
				console.log(array);
			});
			return array;
		},
		resizeLsList: function() {
			$('.lesson-list li').each(function() {
				w = $(this).innerWidth();
				h = w * 4 / 6;
				$(this).css('height', h);
			});

			$(window).resize(function() {
				$('.lesson-list li').each(function() {
					w = $(this).innerWidth();
					h = w * 4 / 6;
					$(this).css('height', h);
				});
			})
		}
	}
})

.factory('catProcess', function($http, BASE_URL) {
	return {
		getCats: function() {
			return $http.post(BASE_URL + 'index.php/application_api/category/getAllCats')
			.then(function(promise) {
				return promise.data;
			})
		}
	}
})